<?php
require '../../common/initPage.php';
require '../../common/dbcon.php';
require '../../common/func.php';
require '../../common/showTips.php';
require '../../common/checkForm.php';

session_start();
//拦截未登录
filterLogout();

if(empty($_GET['keyid'])||!empty($_GET['init'])){
    unset($_SESSION['editUserShowTips']);
}
$editUserInfo=queryInfoByID($mysqlCon,queryisNull,$_GET['keyid']);
if($_GET['action']=='edituser'){
    if(
        isNull(array($_POST['edituid'],$_POST['edituname'])) &&
        checkUserID($_POST['edituid']) &&
        checkUsername($_POST['edituname']) &&
        (empty($_POST['editupwd']) || checkPassword($_POST['editupwd'])) &&
        $_POST['editupwd']==$_POST['editcomupwd'] &&
        (empty($_FILES["editotherupic"]["name"]) || checkUserPic($_FILES["editotherupic"]))
    ){
        $edituid=handledata($_POST['edituid']);
        $edituname=handledata($_POST['edituname']);
        $editusex=handledata($_POST['editusex']);
        $edituiden=handledata($_POST['edituiden']);
        $editudesc=handledata($_POST['editudesc']);
        //如果不输入新密码
        if(empty($_POST['editupwd'])){
            //盐值不变
            $edituslat=$_POST['editoldusalt'];
            //密码也不变
            $editupwd=$_POST['editoldupwd'];
        }else{
            //产生新的盐值
            $edituslat=substr(uniqid(microtime()), -6);
            //产生新的密码
            $editupwd=enpwd(handledata($_POST['editupwd']), $edituslat);
        }
        if(empty($_FILES["editotherupic"]["name"])){
            $editupic=$_POST['editoldupic'];
        }else{
            $editupic=get_randomFileName($_FILES["editotherupic"]["name"],6);
            unlink('../../uploads/userpic/'.$editupic);
            move_uploaded_file($_FILES["editotherupic"]["tmp_name"], "../../uploads/userpic/".$editupic);
        }
        //先用盐值改学号 再用学号改密码
        if(upAllInfo($mysqlCon,updateUserAllInfo,$edituid,$edituname,$editusex,$edituiden,$editudesc,$editupic,$_POST['editoldusalt'])){
            if(upPWDInfo($mysqlCon,updateUserPWDInfo,$editupwd,$edituslat,$edituid)){
                $_SESSION['actionUserShowTips']='修改用户 '.$edituname.' 信息成功';
                $_SESSION['actionUsertipsType']='success';
                header('location:../public/userList.php?show=true&pagenow='.$_SESSION['userListPageNow'].'&searchvalue='.$_SESSION['userListSearchvalue']);
            }else{
                $_SESSION['editUserShowTips'][]='修改密码失败';
                $_SESSION['editUsertipsType']='danger';
                //回滚SQL事务
                header('location:./edituser.php?keyid='.$_POST['editolduid']);
            }
        }else{
            $_SESSION['editUserShowTips'][]='学号/工号已存在';
            $_SESSION['editUsertipsType']='danger';
            header('location:./edituser.php?keyid='.$_POST['editolduid']);
        }
    }else{
        if (!$_POST['edituid']) $_SESSION['editUserShowTips'][]='用户学号/工号不能为空';
        if (!$_POST['edituname']) $_SESSION['editUserShowTips'][]='用户名不能为空';
        if (!checkUserID($_POST['edituid'])) $_SESSION['editUserShowTips'][]='学号/工号应由8位纯数字组成';
        if (!checkUsername($_POST['edituname'])) $_SESSION['editUserShowTips'][]='用户名应由2~16位汉字、英文字母、数字、下划线组成';
        if(!empty($_POST['editupwd'])){
            if (!checkPassword($_POST['editupwd'])) $_SESSION['editUserShowTips'][]='密码应由6~16位英文字母，数字，下划线组成';
            if ($_POST['editupwd']!=$_POST['editcomupwd']) $_SESSION['editUserShowTips'][]='两次密码输入不一致';
        }
        if(!empty($_FILES["editotherupic"]["name"])){
            if (!checkUserPic($_FILES["editotherupic"])) $_SESSION['picInfoShowTips'][]='非法的图片格式';
        }
        if ($_FILES["editupic"]["error"] > 0 && $_FILES["editupic"]["error"] !=4 ){
            switch ($_FILES["editotherupic"]['error']) {
                case 1:
                    $_SESSION['editUserShowTips'][] = '图片大小超过了php.ini中upload_max_filesize选项限制的值';
                    break;
                case 2:
                    $_SESSION['editUserShowTips'][] = '图片大小超过了表单中max_file_size选项指定的值';
                    break;
                case 3:
                    $_SESSION['editUserShowTips'][] = '图片只有部分被上传';
                    break;
                case 6:
                    $_SESSION['editUserShowTips'][] = '找不到临时文件夹';
                    break;
                case 7:
                    $_SESSION['editUserShowTips'][] = '图片写入失败';
                    break;
            }
        }
        $_SESSION['editUsertipsType']='warning';
        header('location:./edituser.php?keyid='.$_POST['editolduid']);
    }
}

$routerUrl='../../views/admin/edituser.html';
require '../../views/admin/template.html';